﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using NexTrackDesktop.NexTrack;

namespace NexTrackDesktop.Media
{
    /// <summary>
    /// This class stores all playlist related info and events.
    /// </summary>
    public class Playlist : NexTrackDesktop.Media.IPlaylist
    {
        private List<Song> _playlist;

        public ReadOnlyCollection<Song> Items
        {
            get
            {
                return new ReadOnlyCollection<Song>(_playlist);
            }
        }

        public Playlist(MainServiceAdapter serviceAdapter)
        {
            _playlist = new List<Song>();
            this.serviceAdapter = serviceAdapter;
        }

        /// <summary>
        /// This method adds the specified song to the playlist.
        /// </summary>
        /// <param name="song">Song to add to playlist</param>
        public void AddSong(Song song)
        {
            _playlist.Add(song);
            serviceAdapter.AddSong(song);
            OnPlaylistChanged();
        }

        /// <summary>
        /// This method adds a collection of Song to the playlist
        /// </summary>
        /// <param name="songs">Collection of Song to add to playlist</param>
        public void AddSongs(IEnumerable<Song> songs)
        {
            foreach (Song song in songs)
            {
                if (_playlist.Contains(song))
                    continue;

                _playlist.Add(song);
                serviceAdapter.AddSong(song);
            }
            OnPlaylistChanged();
        }

        /// <summary>
        /// This method removes the specified song from the playlist.
        /// </summary>
        /// <param name="song">Song to be removed from playlist</param>
        public void RemoveSong(Song song)
        {
            _playlist.Remove(song);
            serviceAdapter.RemoveSong(song);
            OnPlaylistChanged();
        }

        // Events triggered when playlist is changed
        public void OnPlaylistChanged()
        {
            if (PlaylistChanged != null)
                PlaylistChanged();
        }

        public event Action PlaylistChanged;
        private MainServiceAdapter serviceAdapter;
    }
}
